//
// Created by PC on 2023/8/29.
// N进制转10进制 (2<=N<=36)
// https://www.luogu.com.cn/problem/B3620
// pass
//


#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int n = 16, ans = 0;
    string num = "2f";
    cin>>n>>num;
    for (int i = 0; i < num.size(); ++i)
    {
        int t = 0;
        if (num[i] >= '0' && num[i] <= '9')
            t = num[i] - '0';
        else if (num[i] >= 'A' && num[i] <= 'Z')
            t = num[i] - 'A' + 10;
        else if (num[i] >= 'a' && num[i] <= 'z')
            t = num[i] - 'a' + 10;
        ans += t*int(pow((n),(num.size()-1-i)));
    }
    cout<<ans<<endl;

    return 0;
}